package com.service;

import java.sql.SQLException;
import java.util.List;

import com.model.Book;
import com.model.Category;
import com.repository.BookRepository;
import com.repository.BookRepositoryImpl;

public class BookServiceImpl implements BookService {
	BookRepository bookRep;
	
	public BookServiceImpl(){
		try {
			this.bookRep = new BookRepositoryImpl();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	//查询图书信息以及分类
	public List<Book> getBook() {
		List<Book> booklist = bookRep.getBook();
		List<Category> categorylist = bookRep.getCategory();
		if(booklist!=null){
			for (Book book : booklist) {
				for (Category category : categorylist) {
					if (book.getCategory_id()==category.getId()) {
						book.setCategory(category);
					}
				}
			}
			return booklist;
		}
		return null;
	}

	//新增图书
	public boolean addBook(Book book) {
		if(bookRep.addBook(book)==1){
			return true;
		}
		return false;
	}

	@Override
	public boolean delBook(long bookId) {
		if(bookRep.delBook(bookId)==1){
			return true;
		}
		return false;
	}

}
